package yushuman.rbac.service.impl;

import yushuman.rbac.dao.IUserDao;
import yushuman.rbac.dao.file.UserDao;
import yushuman.rbac.domain.dto.UserDto;
import yushuman.rbac.domain.entity.Dept;
import yushuman.rbac.domain.entity.Role;
import yushuman.rbac.domain.entity.User;
import yushuman.rbac.domain.vo.Result;
import yushuman.rbac.domain.vo.UserVo;
import yushuman.rbac.service.IDeptService;
import yushuman.rbac.service.IRoleService;
import yushuman.rbac.service.IUserService;
import yushuman.rbac.util.SingletonFactory;

import java.time.LocalDateTime;
import java.util.Collection;


/**
 * 业务层-用户
 *
 * @author 俞舒熳
 * @since 2025/6/5 08:51
 */

public class UserService extends BaseService<User, UserDto, UserVo, IUserDao> implements IUserService {

    private final IDeptService deptService;
    private final IRoleService roleService;

    private UserService(){
        deptService = SingletonFactory.getInstance(DeptService.class);
        roleService = SingletonFactory.getInstance(RoleService.class);
        dao = SingletonFactory.getInstance(UserDao.class);
        temp = new UserVo[0];
    }

    @Override
    UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }

        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }

        return new UserVo(user.getId(), user.createAt, user.updateAt, user.username, user.name, user.mobile, user.email,
                user.deptId, deptName, user.roleId, roleName, user.enabled);

    }

    @Override
    User createEntity(UserDto dto) {
        LocalDateTime now = LocalDateTime.now();
        return new User(null, now, now, dto.username, dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }

    @Override
    void updateEntity(UserDto dto, User user) {
        user.updateAt = LocalDateTime.now();
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public boolean deptInUse(int deptId){
        boolean inUse = false;
        Collection<User> values = dao.list();
        for (User user : values) {
            if (user != null && user.deptId == deptId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleId){
        boolean inUse = false;
        Collection<User> values = dao.list();
        for (User user : values) {
            if (user != null && user.roleId == roleId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public Result create(UserDto dto) {
        if (!deptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!roleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定的角色");
        }
        return super.create(dto);
    }

    @Override
    public Result update(UserDto dto, int id){
        if (!deptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!roleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定的角色");
        }
        return super.update(dto, id);
    }

    @Override
    public Result toggle(int id) {
        if(!existsById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        User user = dao.findById(id);
        user.enabled = !user.enabled;
        user = dao.save(user);
        return Result.success(user.enabled ? "启用成功" : "停用成功", null);
    }
}
